package mwt.xml.xdbforms.schemalayer.types;

import javax.xml.XMLConstants;
import mwt.xml.xdbforms.dblayer.metadata.impl.ColumnMetaData;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;

/**
 *
 * Un tipo MEDIUM INT viene trattato alla
 * stregua di un INT
 * <i>
 * pecurialità del database
 * </i>.
 *
 * @author Gianfranco Murador
 */
public class XmlInteger extends XmlTypes {

    public XmlInteger(Document doc) {
        super(doc);
    }

    public XmlInteger() {
    }

    private void buildFragment(ColumnMetaData cmd) {

        Element xtype;
        Element restriction, maxInclusive, minInclusive;
        fragment = doc.createDocumentFragment();


        xtype = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, "xs:simpleType");
        xtype.setAttribute("name", typeName);

        cmd.setXschemaTypeName(typeName);


        restriction = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, "xs:restriction");
        restriction.setAttribute("base", "xs:integer");

        maxInclusive = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, "xs:maxInclusive");
        maxInclusive.setAttribute("value", Integer.toString(Integer.MAX_VALUE));
        minInclusive = doc.createElementNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, "xs:minInclusive");
        minInclusive.setAttribute("value", Integer.toString(Integer.MIN_VALUE));

        // Costruisci il sottoalbero

        xtype.appendChild(restriction);
        restriction.appendChild(maxInclusive);
        restriction.appendChild(minInclusive);
        fragment.appendChild(xtype);

    }

    @Override
    public DocumentFragment getSchemaType(ColumnMetaData cmd) {


        typeName = cmd.getTypeName();

        if (!typeName.equals("INT")) {

            // Controllare se è una richiesta alle sue sottoclassi
            // BigInt SmallInt TinyInt
            if (typeName.equals("MEDIUMINT")) {
                // viene trattato come un INT

                buildFragment(cmd);
                return fragment;


            } else {

                if (typeName.equals("SMALLINT")) {
                    // short
                    return new XmlSmallInt(doc).getSchemaType(cmd);

                } else {

                    if (typeName.equals("BIGINT")) {
                        // Long

                        return new XmlBigInt(doc).getSchemaType(cmd);
                    } else {

                        if (typeName.equals("TINYINT")) {

                            return new XmlTinyInt(doc).getSchemaType(cmd);

                        } else {

                            return nextType.getSchemaType(cmd);

                        }


                    }
                }


            }


        } else {

            // Costruisci il tipo di dato integer
            buildFragment(cmd);
            return fragment;
        }
    }

    @Override
    public Object getXSchemaToJavaType(String baseType, String value) {

        if (!baseType.startsWith("INT")) {

            // Controllare se è una richiesta alle sue sottoclassi
            // BigInt SmallInt TinyInt
            if (baseType.startsWith("MEDIUMINT")) {
                // viene trattato come un INT

                Integer i = Integer.parseInt(value);
                return i;


            } else {

                if (baseType.startsWith("SMALLINT")) {
                    // short
                    return new XmlSmallInt().getXSchemaToJavaType(baseType, value);

                } else {

                    if (baseType.startsWith("BIGINT")) {
                        // Long
                        return new XmlBigInt().getXSchemaToJavaType(baseType, value);
                    } else {

                        if (baseType.startsWith("TINYINT")) {

                            return new XmlTinyInt().getXSchemaToJavaType(baseType, value);

                        } else {

                            return nextType.getXSchemaToJavaType(baseType, value);

                        }


                    }
                }


            }


        } else {

            // Costruisci il tipo di dato integer
            Integer i = Integer.parseInt(value);
            return i;

        }
    }
}

